﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ChinaClipping.ClippingMaster.Editor
{
    public partial class Dlg_InsertTable : Form
    {
        private static Dlg_InsertTable _dialog = new Dlg_InsertTable();

        private string _bordercolor, _bgcolor;
        private StringBuilder _sb=new StringBuilder (500);
        private Dlg_InsertTable()
        {
            InitializeComponent();
        }

        public static Dlg_InsertTable GetDialog()
        {
            _dialog.Cbx_Align.SelectedIndex = 0;
            _dialog.Cbx_WidthType.SelectedIndex = 0;
            _dialog._bordercolor = "#000000";
            _dialog._bgcolor = "#ffffff";
            return _dialog;
        }

        private void OK_Button_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void Btn_SetBorderColor_Click(object sender, EventArgs e)
        {
            if (FontColor.ShowDialog() == DialogResult.OK)
            {
                Btn_SetBorderColor.BackColor = FontColor.Color;
                _bordercolor = string.Format("#{0}{1}{2}", FontColor.Color.R.ToString("X2"), FontColor.Color.G.ToString("X2"), FontColor.Color.B.ToString("X2"));
            }
        }

        private void Btn_SetBackColor_Click(object sender, EventArgs e)
        {
            if (FontColor.ShowDialog() == DialogResult.OK)
            {
                Btn_SetBackColor.BackColor = FontColor.Color;
                _bgcolor = string.Format("#{0}{1}{2}", FontColor.Color.R.ToString("X2"), FontColor.Color.G.ToString("X2"), FontColor.Color.B.ToString("X2"));
            }
        }

        private void Chbx_AppointWidth_CheckedChanged(object sender, EventArgs e)
        {
            Numeric_TableWidth.Enabled = Chbx_AppointWidth.Checked;
            Cbx_WidthType.Enabled = Chbx_AppointWidth.Checked;
        }

        public string TableHtml
        {
            get 
            {
                _sb.Remove(0, _sb.Length);
                _sb.Append("<TABLE align=");
                switch (Cbx_Align.Text)
                {
                    case "居左":
                        _sb.Append("left");
                        break;
                    case "居中":
                    _sb.Append("center");
                    break;
                    case "居右":
                    _sb.Append("right");
                    break;
                }
                _sb.Append(" border=");
                _sb.Append(Numeric_Border.Value);
                _sb.Append(" cellpadding=");
                _sb.Append(Numeric_CellPadding.Value);
                _sb.Append(" cellspacing=");
                _sb.Append(Numeric_CellSpace.Value);
                _sb.Append(" borderColor=");
                _sb.Append(_bordercolor);
                _sb.Append(" bgcolor=");
                _sb.Append(_bgcolor);
                if (Chbx_AppointWidth.Checked)
                {
                    _sb.Append(" width=");

                    switch (Cbx_WidthType.Text)
                    { 
                        case "象素":
                            _sb.Append(Numeric_TableWidth.Value);
                            break;
                        case "百分比":
                            _sb.Append(Numeric_TableWidth.Value);
                            _sb.Append("%");
                            break;
                    }
                }
                _sb.Append(">");

                for (int i = 1; i <= Numeric_RowNumber.Value; i++)
                {
                    _sb.Append("<TR>");
                    for (int j = 1; j <= Numeric_ColNumber.Value; j++)
                    {
                        _sb.Append("<TD></TD>");
                    }
                    _sb.Append("</TR>");
                
                }
                _sb.Append("</TABLE>");
                return _sb.ToString();
            }
        }
    }
}
